#! /bin/sh
#################################################################
##Description: This script is used to compare the EulerLinux.tar.gz
#################################################################

CompareDir=`pwd`
Compare_file="$1"
Compare1_dir="$2"
Compare2_dir="$3"
DecompressDir="${CompareDir}/compare_workspace"
DifferentFile="${CompareDir}/Difference"
#给出部分text文件，特殊差异原因。如果发现新的text格式的文件存在其他特殊的差异原因，也可以在这里补充
spec_text_diff_files=""
spec_text_diff_files+=".checksum.md5\tkiwi是无序检查文件的md5码，所以顺序上有差别;\n"
spec_text_diff_files+="crypto.lst\t该文件存放的是加密模块，通过python语言的os.list接口读某个目录下的文件列表来生成的，os.list返回的列表是没有经过排序的，因此两次编译生成的内容次序先后是不一样的;\n"
spec_text_diff_files+="mtab\t不同环境的挂载分区表差异;\n"
spec_text_diff_files+="libdpdk.a TRANS.TBL docker.pp System.map\t编译环境不同导致排序差异;\n"
spec_text_diff_files+=".depend.boot .depend.start .depend.halt .depend.stop modules.pcimap modules.dep modules.alias modules.usbmap modules.symbols\t使用depmod命令导致排序差异;\n"
spec_text_diff_files+="dracut.html\thtml idm值唯一;\n"
spec_text_diff_files+="repomd.xml\t编译时间差异;hash值差异;压缩包大小差异;\n"
spec_text_diff_files+="filelists.xml\thash值差异;不同环境文件排序差异;\n"
spec_text_diff_files+="primary.xml\t编译时间差异;hash值差异;编译环境名差异;不同环境文件排序差异;压缩包大小差异;\n"
spec_text_diff_files+="HOSTNAME hosts\t编译环境名差异;\n"
spec_text_diff_files+="default.xml\t环境分配的虚拟机ID差异;\n"
spec_text_diff_files+="initiatorname.iscsi\tiscsi target名称差异;\n"
spec_text_diff_files+="machine-id\t机器ID差异;\n"
spec_text_diff_files+="shadow shadow.old shadow.aaa_base user-haldaemon.auths\t编译时间不同导致的差异;\n"

#检查入参个数
function usage()
{
	if [ $# -ne 3 ];then
		echo "You must input the correct parameters: the name of file or directory,directory of compare1,directory of compare2."
		echo "e.g: bash diff_euler.sh EulerLinux.tar.gz compare1 compare2"
		exit 1
	fi
}
#检查对比的文件是否存在
function check_file_exist()
{
	if [ ! -e "$CompareDir"/"$Compare1_dir"/"$Compare_file" ]
	then
		echo "ERROR: "$Compare1_dir"/"$Compare_file" is not exist"
		exit 1
	fi

	if [ ! -e "$CompareDir"/"$Compare2_dir"/"$Compare_file" ]
	then
		echo "ERROR: "$Compare2_dir"/"$Compare_file" is not exist"
		exit 1
	fi

}
#清理环境
function clean_env()
{
	if echo ${Compare_file} | grep ".iso$"
	then
		#local Name=${Compare_file%.iso}
		umount ${CompareDir}/${Compare1_dir}/${Compare_file} 1>/dev/null 2>&1
		umount ${CompareDir}/${Compare2_dir}/${Compare_file} 1>/dev/null 2>&1
		rm -rf `ls ${Compare1_dir} | grep -v ${Compare_file}`
		rm -rf `ls ${Compare2_dir} | grep -v ${Compare_file}`
	else
		rm -rf `ls ${Compare1_dir} | grep -v ${Compare_file}`
		rm -rf `ls ${Compare2_dir} | grep -v ${Compare_file}`
	fi
	rm -rf ${DecompressDir} ${DifferentFile}
	mkdir -p ${DecompressDir}/${Compare1_dir} ${DecompressDir}/${Compare2_dir}
}
#挂载iso文件，并找到其中所有的文件，开始对比
function decompress_iso()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local base_file=`basename "$file"`
	local name=${base_file%.iso}
	local filelist1=${name}_list1
	local filelist2=${name}_list2

	mkdir -p ${DecompressDir}/${Compare1_dir}/${name}
	mkdir -p $CompareDir/tmp
	mount ${compare_dir_1}/"${file}" $CompareDir/tmp
	if [ $? -ne 0 ];then
		echo "decompress ${compare_dir_1}/"${file}" failed!"
		exit 1
	fi
	cp -rf $CompareDir/tmp/* ${DecompressDir}/${Compare1_dir}/${name}
	umount $CompareDir/tmp;rm -rf $CompareDir/tmp

	mkdir -p ${DecompressDir}/${Compare2_dir}/${name}
	mkdir -p $CompareDir/tmp
	mount ${compare_dir_2}/"${file}" $CompareDir/tmp
	if [ $? -ne 0 ];then
		echo "decompress ${compare_dir_2}/"${file}" failed!"
		exit 1
	fi
	cp -rf $CompareDir/tmp/* ${DecompressDir}/${Compare2_dir}/${name}
	umount $CompareDir/tmp;rm -rf $CompareDir/tmp

	pushd ${DecompressDir}/${Compare1_dir}/"${name}" &>/dev/null
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist1"
	popd &>/dev/null
	pushd ${DecompressDir}/${Compare2_dir}/"${name}" &>/dev/null
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist2"
	popd &>/dev/null
	compare_dir "$filelist1" "$filelist2" ${name}
	return $?
}
#对比链接文件
function compare_link()
{
	local file="$1"
	local compare_dir_1="$2"
	local compare_dir_2="$3"
	local path=`dirname "$file"`
	echo $compare_dir_1/"$file" | egrep "/dev/|/udev/" &>/dev/null
	if [ $? -eq 0 ]
	then
		return 0
	fi
	mkdir -p $DecompressDir/compare1/$path
	mkdir -p $DecompressDir/compare2/$path
	cp $compare_dir_1/"$file" $DecompressDir/compare1/$path 2> /dev/null
	local ret1=$?
	cp $compare_dir_2/"$file" $DecompressDir/compare2/$path 2> /dev/null
	local ret2=$?
	if [ $ret1 -eq 1 -a $ret2 -eq 1 ]
	then
		return 0
	elif [ $ret1 -eq 0 -a $ret2 -eq 0 ]
	then
		compare_file "$file" $DecompressDir/compare1 $DecompressDir/compare2
		return $?
	else
		echo -e "${file}\t文件在版本中不存在" | tee -a ${DifferentFile}
	fi
}
#对比部分特殊的，已有结论的文件
function yamdb_diff()
{
	local file="$1"
	local name=`basename "$file"`
	if [ "$name" == "from_repo_revision" ] || [ "$name" == "from_repo_timestamp" ]
	then
		echo "编译时间导致差异;"
		return 0
	elif [ "$name" == "checksum_data" ] || [ "$name" == "var_uuid" ]
	then
		echo "文件中包含共享库的hmac hashes值，共享库不同，hash值也不同，对二进制没有影响;"
		return 0
	elif [ "$name" == "checksum_type" ] || [ "$name" == "command_line" ] || [ "$name" == "releasever" ] || [ "$name" == "from_repo" ] || [ "$name" == "origin_url" ] || [ "$name" == "reason" ]
	then
		echo "目录包含hash值，文件内容无差异;"
		return 1
	elif [ "$name" == "installed_by" ]
	then
		echo "第一次安装时用户的uuid不同;"
		return 0
	else
		return 2
	fi
}
#对比目录
function compare_dir()
{
	sleep 1
	local compare_dir_1=${DecompressDir}/${Compare1_dir}
	local compare_dir_2=${DecompressDir}/${Compare2_dir}
	local compare_dir="$3"
	local name=`basename $3`
	local filelist=${name}_filelist.`date +%s`
	local filelist1=$1
	local filelist2=$2
	local line=""
	local ret=0
	local ret1=0
	local diff_reason1=""
	local diff_reason2=""

	while read line
	do
		if [ -L ${compare_dir_1}/"${compare_dir}"/"$line" ] && [ -L ${compare_dir_2}/"${compare_dir}"/"$line" ]
		then
			#compare_link "$line" "${compare_dir_1}/${compare_dir}" "${compare_dir_2}/${compare_dir}"
			#ret=$?
			continue
		fi
		if [  -d ${compare_dir_1}/"${compare_dir}"/"$line" ]
		then
			continue
		fi
		if [ ! -e ${compare_dir_2}/"${compare_dir}"/"$line" ]
		then
			diff_reason1=`yamdb_diff "$line"`
			ret1=$?
			if [ $ret1 -eq 0 ]
			then
				diff_reason2="目录包含hash值;"
			elif [ $ret1 -eq 1 ]
			then
				diff_reason2=""
			elif [ $ret1 -eq 2 ]
			then
				diff_reason1=""
				diff_reason2=""
			fi

			echo -e "${line}\t${diff_reason1}${diff_reason2}" | tee -a ${DifferentFile}
			ret=1
		else
			if [ ! -d ${compare_dir_2}/"${compare_dir}"/"$line" ]
			then
				echo "${line}" >> ${DecompressDir}/${filelist}
			fi
		fi
	done < ${DecompressDir}/${filelist1}
	rm ${DecompressDir}/${filelist1}

	while read line
	do
		if [ -L ${compare_dir_1}/"${compare_dir}"/"$line" ] && [ -L ${compare_dir_2}/"${compare_dir}"/"$line" ]
		then
			#compare_link "$line" "${compare_dir_1}/${compare_dir}" "${compare_dir_2}/${compare_dir}"
			#ret=$?
			continue
		fi
		if [  -d ${compare_dir_2}/"${compare_dir}"/"$line" ]
		then
			continue
		fi
		if [ ! -e ${compare_dir_1}/"${compare_dir}"/"$line" ]
		then
			diff_reason1=`yamdb_diff "$line"`
			ret1=$?
			if [ $ret1 -eq 0 ]
			then
				diff_reason2="目录包含hash值;"
			elif [ $ret1 -eq 1 ]
			then
				diff_reason2=""
			elif [ $ret1 -eq 2 ]
			then
				diff_reason1=""
				diff_reason2=""
			fi

			echo -e "${line}\t${diff_reason1}${diff_reason2}" | tee -a ${DifferentFile}
			ret=1
		fi
	done < ${DecompressDir}/${filelist2}
	rm ${DecompressDir}/${filelist2}

	if [ ! -e ${DecompressDir}/${filelist} ]
	then
		return $ret
	fi

	while read line
	do
		compare_file "${line}" ${compare_dir_1}/${compare_dir} ${compare_dir_2}/${compare_dir}
		if [ $? -ne 0 ]
		then
			ret=1
		fi
	done < ${DecompressDir}/${filelist}
	rm -rf ${DecompressDir}/${filelist}
	return $ret
}
#对比iso文件
function compare_iso()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3

	decompress_iso "${file}" $compare_dir_1 $compare_dir_2
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#解压tag包，找到其中所有的文件，开始对比
function decompress_tar()
{
	local file="$1"
	local path=`dirname "$file"`
	local base_file=`basename "$file"`
	local compare_dir_1=$2
	local compare_dir_2=$3
	local filelist1=${base_file}_list1
	local filelist2=${base_file}_list2

	tar -tvf ${compare_dir_1}/"${file}" | awk 'BEGIN {FS=" ";ORS=" "} {for(i=6;i<=NF;i++)print($i)}{print "\n"}' | sed 's/^ //g' | sed "s| ->.*$||g" | sed "s| link to.*$||g" >> ${DecompressDir}/${filelist1}
	tar -tvf ${compare_dir_2}/"${file}" | awk 'BEGIN {FS=" ";ORS=" "} {for(i=6;i<=NF;i++)print($i)}{print "\n"}' | sed 's/^ //g' | sed "s| ->.*$||g" | sed "s| link to.*$||g" >> ${DecompressDir}/${filelist2}

	mkdir -p ${DecompressDir}/${Compare1_dir}/${path}
	pushd ${DecompressDir}/${Compare1_dir}/${path} &>/dev/null
	cp -rf ${compare_dir_1}/"${file}" ${DecompressDir}/${Compare1_dir}/${path} 2>/dev/null
	tar xf ${base_file}
	if [ $? -ne 0 ];then
		echo "decompress ${compare_dir_1}/"${file}" failed!"
		exit 1
	fi
	rm -rf ${base_file}
	popd &>/dev/null

	mkdir -p ${DecompressDir}/${Compare2_dir}/${path}
	pushd ${DecompressDir}/${Compare2_dir}/${path} &>/dev/null
	cp -rf ${compare_dir_2}/"${file}" ${DecompressDir}/${Compare2_dir}/${path} 2>/dev/null
	tar xf ${base_file}
	if [ $? -ne 0 ];then
		echo "decompress ${compare_dir_2}/"${file}" failed!"
		exit 1
	fi
	rm -rf ${base_file}
	popd &>/dev/null
	compare_dir "$filelist1" "$filelist2" ${path}
	return $?
}
#对比tar压缩文件
function compare_tar()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3

	decompress_tar "${file}" $compare_dir_1 $compare_dir_2
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#解压rpm包，并对比其中的文件
function compare_rpm()
{
	local file="$1"
	local path=`dirname "$file"`
	local base_file=`basename "$file"`
	local compare_dir_1=$2
	local compare_dir_2=$3
	local name=${file%.rpm}
	local filelist1=${base_file}_list1
	local filelist2=${base_file}_list2

	mkdir -p ${DecompressDir}/${Compare1_dir}/${name}
	pushd ${DecompressDir}/${Compare1_dir}/${name} &>/dev/null
	cp ${compare_dir_1}/"${file}" ${DecompressDir}/${Compare1_dir}/${name}
	rpm2cpio ${base_file} | cpio -di &>/dev/null
	rm ${base_file}
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist1"
	popd &>/dev/null

	mkdir -p ${DecompressDir}/${Compare2_dir}/${name}
	pushd ${DecompressDir}/${Compare2_dir}/${name} &>/dev/null
	cp ${compare_dir_2}/"${file}" ${DecompressDir}/${Compare2_dir}/${name}
	rpm2cpio ${base_file} | cpio -di &>/dev/null
	rm ${base_file}
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist2"
	popd &>/dev/null

	compare_dir "$filelist1" "$filelist2" ${name}
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#如果开始的时候，对比的是目录，那就备份这个目录中的文件再对比。
function backup_dir()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local path=`dirname "$file"`
	local base_file=`basename "$file"`
	local filelist1=${base_file}_list1
	local filelist2=${base_file}_list2

	mkdir -p ${DecompressDir}/${Compare1_dir}/"$file"
	cp -rf "$compare_dir_1"/"$file"/* ${DecompressDir}/${Compare1_dir}/"$file"
	pushd ${DecompressDir}/"$Compare1_dir"/"$path" &>/dev/null
	find $base_file >> ${DecompressDir}/"$filelist1"
	popd &>/dev/null

	mkdir -p ${DecompressDir}/${Compare2_dir}/"$file"
	cp -rf "$compare_dir_2"/"$file"/* ${DecompressDir}/${Compare2_dir}/"$file"
	pushd ${DecompressDir}/"$Compare2_dir"/"$path" &>/dev/null
	find $base_file >> ${DecompressDir}/"$filelist2"
	popd &>/dev/null
	compare_dir "$filelist1" "$filelist2" $path
	return $?
}
#对比压缩文件
function compare_compress_file()
{
	local compress_type="$1"
	local file="$2"
	local compare_dir_1=$3
	local compare_dir_2=$4

	case $compress_type in
		gunzip)
		local suffix="gz"
		;;
		bunzip2)
		local suffix="bz2"
		;;
		unxz)
		local suffix="xz"
		;;
		*)
		;;
	esac

	if [ `echo ${file##*.}` == $suffix ]
	then
		local name=${file%.$suffix}
		$compress_type -f "$compare_dir_1"/"$file"
		$compress_type -f "$compare_dir_2"/"$file"
		compare_file $name $compare_dir_1 $compare_dir_2
		return $?
	else
		mv "$compare_dir_1"/"$file" "$compare_dir_1"/""${file}".$suffix"
		mv "$compare_dir_2"/"$file" "$compare_dir_2"/""${file}".$suffix"
		$compress_type -f "$compare_dir_1"/""${file}".$suffix"
		$compress_type -f "$compare_dir_2"/""${file}".$suffix"
		compare_file "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
}
#分析text格式的文件的差异原因
function find_text_diff_reason()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local name=`basename "$file"`
	# 2018-07-16
	local time_stamp_type1="20[0-9]{2}-[0-9]{2}-[0-9]{2}"
	# 16/07/2018
	local time_stamp_type2="[0-9]{2}/[0-9]{2}/20[0-9]{2}"
	# Jul 16 15:20:16
	local time_stamp_type3="(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2} ([0-9]{2}*:*){3}"
	# 2018_05_23_18_15
	local time_stamp_type4="20[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}"
	# 20180716091535/201807121839
	local time_stamp_type5="20[0-9]{2}[01][0-9][0-3][0-9][0-2][0-9][0-6][0-9]"
	# July 10, 2018
	local time_stamp_type6="(January|February|March|April|May|June|July|August|September|October|November|December) [0-3][0-9], 20[0-9]{2}"
	# 2018.07.10
	local time_stamp_type7="20[0-9]{2}\.[0-1][0-9]\.[0-3][0-9]"
	# June 2018
	local time_stamp_type8="(January|February|March|April|May|June|July|August|September|October|November|December) 20[0-9]{2}"
	# 10 July
	local time_stamp_type9="[0-3][0-9] (January|February|March|April|May|June|July|August|September|October|November|December)"
	# 2018.197.755 第一个字符串代表年，第二个代表编译时间距离1月1日的天数，第三个代表编译时间距离0时0分的的分钟数
	local time_stamp_type10="20[0-9]{2}\.[0-9]{1,3}\.[0-9]{1,4}"
	# 12.02.2019
	local time_stamp_type11="[0-9]{2}\.[0-9]{2}\.20[0-9]{2}"
	# 12-02-2019
	local time_stamp_type12="[0-9]{2}-[0-9]{2}-20[0-9]{2}"
	local compile_evn_type1="LINUX_COMPILE_HOST"
	# build10b184b205b65
	local compile_evn_type2="build[0-9]{1,3}b[0-9]{1,3}b[0-9]{1,3}b[0-9]{1,3}"
	# build10a184a205a65
	local compile_evn_type3="build[0-9]{1,3}a[0-9]{1,3}a[0-9]{1,3}a[0-9]{1,3}"
	local hash_type1="[0-9a-f]{40}"
	#分析部分已有结论的文件
	yamdb_diff "$file"
	if [ $? -ne 2 ]
	then
		return
	fi
	#文件后缀为el的文件差异原因
	if [ `echo ${file##*.}` == "el" ]
	then
		echo "环境不同导致的差异;"
		return
	fi
	#部分已有结论的文件差异原因。
	echo -e "$spec_text_diff_files" | grep "$name" &>/dev/null
	if [ $? -eq 0 ]
	then
		echo -e "$spec_text_diff_files" | grep "$name" | awk 'BEGIN {FS="\t"} {print $NF}'
		return
	fi
	#分析是否是时间戳差异
	diff "$compare_dir_1"/"$file" "$compare_dir_2"/"$file" | egrep "$time_stamp_type1|$time_stamp_type2|$time_stamp_type3|$time_stamp_type4|$time_stamp_type5|$time_stamp_type6|$time_stamp_type7|$time_stamp_type8|$time_stamp_type9|$time_stamp_type10|$time_stamp_type11|$time_stamp_type12" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "时间戳差异;"
	fi
	#分析是否是编译环境名差异。
	diff "$compare_dir_1"/"$file" "$compare_dir_2"/"$file" | egrep "$compile_evn_type1|$compile_evn_type2|$compile_evn_type3" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "编译环境名差异;"
		return
	fi
	#后缀为hmac的文件差异原因。
	if [ `echo ${file##*.}` == "hmac" ]
	then
		echo "文件中包含共享库的hmac hashes值，共享库不同，hash值也不同，对二进制没有影响;"
		return
	fi
	#分析文件差异内容是否是hash值差异。
	diff "$compare_dir_1"/"$file" "$compare_dir_2"/"$file" | egrep "$hash_type1" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "哈希值引起差异;"
		return
	fi
}
#对比text格式的文件
function compare_text()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local diff_reason=''

	diff_reason=`find_text_diff_reason "$file" "$compare_dir_1" "$compare_dir_2"`
	diff_reason=`echo $diff_reason | sed 's/\n/ /g'`
	if [ -z "$diff_reason" ]
	then
		return 0
	else
		echo -e ""$file"\t"$diff_reason"" | tee -a ${DifferentFile}
		return 1
	fi
}
#剥离二进制文件对应段的内容
function clean_section()
{
	local head_name="$1"
	local file="$2"
	local ret=""
	local name=`basename "$file"`

	for ret in 1 2
	do
		local line=0
		local line1=0
		local line2=0
		line1=`grep -n "Disassembly" ${DecompressDir}/"$name".text$ret | grep ' \\'$head_name':' | awk 'BEGIN {FS=":"} {print $1}'`
		if [ -z $line1 ]
		then
			continue
		fi
		for line in `grep -n Disassembly ${DecompressDir}/"$name".text$ret | awk 'BEGIN {FS=":"} {print $1}'`
		do
			if [ $line -gt $line1 ]
			then
				line2=$line
				line2=`expr $line2 - 1`
				break
			fi
		done
		if [ $line2 -eq 0 ]
		then
			line2=`wc -l ${DecompressDir}/"$name".text$ret |awk '{print $1}'`
		fi
		sed -i ""$line1","$line2"d" ${DecompressDir}/"$name".text$ret
	done
}
#分析二进制文件数据段的差异原因
function find_elf_data_diff_reason()
{
	local type="$1"
	#abuild_2018_07_10_09_14
	local time_diff_reason1="abuild_20[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}"
	#Jul 10 2018
	local time_diff_reason2="(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9\ ]{2} 20[0-9]{2}"
	#2018-05-31
	local time_diff_reason3="20[0-9]{2}-[0-9]{2}-[0-9]{2}"
	#Tue Jul 10 09:13:05 UTC 2018
	local time_diff_reason4="(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9\ ]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3} 20[0-9]{2}"
	#16 July 2018
	local time_diff_reason5="[0-9]{2} (January|February|March|April|May|June|July|August|September|October|November|December) 20[0-9]{2}"
	#05:11:12.Jul 16 2018
	local time_diff_reason6="[0-9]{2}:[0-9]{2}:[0-9]{2}\.(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9\ ]{2} 20[0-9]{2}"
	#Mon Jul 16 09:52:56 2018
	local time_diff_reason7="(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9\ ]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} 20[0-9]{2}"
	#20180710092347
	local time_diff_reason8="20[0-9]{2}[0-1][0-9][0-3][0-9][0-2][0-9][0-6][0-9][0-6][0-9]"
	# July 25, 2017
	local time_diff_reason9="(January|February|March|April|May|June|July|August|September|October|November|December) [0-3][0-9], 20[0-9]{2}"
	#2018.07.11.02:46:04
	local time_diff_reason10="20[0-9]{2}\.[0-1][0-9]\.[0-3][0-9]\.[0-2][0-9]:[0-6][0-9]:[0-6][0-9]"
	# 2018.197.755 第一个字符串代表年，第二个代表编译时间距离1月1日的天数，第三个代表编译时间距离0时0分的的分钟数
	local time_diff_reason11="20[0-9]{2}\.[0-9]{1,3}\.[0-9]{1,4}"
	#build10b184b205b65
	local build_env_name="build[0-9]{1,3}b[0-9]{1,3}b[0-9]{1,3}b[0-9]{1,3}"
	local hash_num_diff_reason="[0-9a-f]{40}"
	local flag=0
	local ret=`sed -n '3p' $DecompressDir/1.log | awk 'BEGIN {FS="  "} {print $1}' | wc -c`
	ret=`expr $ret + 2`
	diff -uN $DecompressDir/1.log $DecompressDir/2.log | sed '1,3d' | sed "s/^.\{$ret\}//g" | tr "\n" "," | sed 's/,//g' | egrep "$time_diff_reason1|$time_diff_reason2|$time_diff_reason3|$time_diff_reason4|$time_diff_reason5|$time_diff_reason6|$time_diff_reason7|$time_diff_reason8|$time_diff_reason9|$time_diff_reason10|$time_diff_reason11" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "编译时间差异;"
		flag=1
	fi
	diff -uN $DecompressDir/1.log $DecompressDir/2.log | sed '1,3d' | sed "s/^.\{$ret\}//g" | tr "\n" "," | sed 's/,//g' | egrep "$build_env_name" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "编译环境名差异;"
		flag=1
	fi
	diff -uN $DecompressDir/1.log $DecompressDir/2.log | sed '1,3d' | sed "s/^.\{$ret\}//g" | tr "\n" "," | sed 's/,//g' | egrep "$hash_num_diff_reason" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "hash值差异;"
		flag=1
	fi
	if [ $flag -eq 0 -a $type == "data" ]
	then
		echo "数据段存在差异;"
	elif [ $flag -eq 0 -a $type == "rodata" ]
	then
		echo "只读数据段存在差异;"
	fi
}
#分析二进制文件代码段的差异原因
function find_elf_text_diff_reason()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	rm -rf $DecompressDir/1.log $DecompressDir/2.log

	objdump -d -j .text --no-show-raw-insn "$compare_dir_1"/"$file" 1> $DecompressDir/1.log 2> /dev/null
	objdump -d -j .text --no-show-raw-insn "$compare_dir_2"/"$file" 1> $DecompressDir/2.log 2> /dev/null
	sed -i '1,2d' $DecompressDir/1.log $DecompressDir/2.log 2>/dev/null
	#去掉反汇编后指令的地址,如:"    4d4a:   mov    (%r12),%rdi" -> "mov    (%r12),%rdi"
	sed -i "s|^[[:blank:]]*[[:xdigit:]]\+:[[:blank:]]*||g" $DecompressDir/1.log $DecompressDir/2.log 2>/dev/null
	#去掉指令的操作数，保留汇编指令和函数名,如:"lea    0x196ee(%rip),%rsi	# 1e44f <_fini@@Base+0x2b>" -> "lea <_fini@@Base+0x2b>"
	sed -i "s|[[:blank:]]\+[^<]*| |g" $DecompressDir/1.log $DecompressDir/2.log 2>/dev/null
	#去掉函数中包含的地址,如: "lea <_fini@@Base>"
	sed -i "s|\+.*>|>|g" $DecompressDir/1.log $DecompressDir/2.log 2>/dev/null
	diff $DecompressDir/1.log $DecompressDir/2.log &>/dev/null
	if [ $? -eq 0 ]
	then
		echo "编译时间和编译环境不同导致的地址偏移;"
	else
		echo "代码段存在差异;"
	fi
}
#分析二进制文件是否是因为符号链接顺序导致的差异
function symbols_link_order_diff()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local name=`basename "${file}"`
	local ret1=0
	local ret2=0

	file -b "$compare_dir_1"/"$file" | grep "not stripped" &> /dev/null
	ret1=$?
	file -b "$compare_dir_2"/"$file" | grep "not stripped" &> /dev/null
	ret2=$?
	if [ $ret1 -eq $ret2 -a $ret1 -eq 0 ]
	then
		nm "$compare_dir_1"/"$file" > $DecompressDir/"$name".symbols1
		nm "$compare_dir_2"/"$file" > $DecompressDir/"$name".symbols2
		nm -u "$compare_dir_1"/"$file" | awk '{print $2}' | sort -rn >> $DecompressDir/"$name".symbols_sort1
		nm --defined-only "$compare_dir_1"/"$file" | awk '{print $3}' | sort -rn >> $DecompressDir/"$name".symbols_sort1
		nm -u "$compare_dir_2"/"$file" | awk '{print $2}' | sort -rn >> $DecompressDir/"$name".symbols_sort2
		nm --defined-only "$compare_dir_2"/"$file" | awk '{print $3}' | sort -rn >> $DecompressDir/"$name".symbols_sort2
		diff $DecompressDir/"$name".symbols1 $DecompressDir/"$name".symbols2 &>/dev/null
		ret1=$?
		diff $DecompressDir/"$name".symbols_sort1 $DecompressDir/"$name".symbols_sort2 &>/dev/null
		ret2=$?
		rm -rf $DecompressDir/"$name".symbols1 $DecompressDir/"$name".symbols2 $DecompressDir/"$name".symbols_sort1 $DecompressDir/"$name".symbols_sort2
		if [ $ret1 -ne 0 -a $ret2 -eq 0 ]
		then
			echo "编译环境导致符号链接顺序不同;"
			return 0
		else
			return 1
		fi
	elif [ $ret1 -ne $ret2 ]
	then
		echo "由于包含调试信息导致的差异;"
		return 0
	else
		return 1
	fi
}
#分析二进制文件的差异原因
function find_elf_diff_reason()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local name=`basename "${file}"`
	local line=""

	symbols_link_order_diff "$file" "$compare_dir_1" "$compare_dir_2"
	if [ $? -eq 0 ]
	then
		return 0
	fi

	objdump -D "$compare_dir_1"/"$file" 1> $DecompressDir/"$name".text1 2> /dev/null
	objdump -D "$compare_dir_2"/"$file" 1> $DecompressDir/"$name".text2 2> /dev/null
	sed -i '1,2d' $DecompressDir/"$name".text1 $DecompressDir/"$name".text2

	diff $DecompressDir/"$name".text1 $DecompressDir/"$name".text2 &> /dev/null
	if [ $? -eq 0 ]
	then
		echo "差异原因不明;"
		rm -rf $DecompressDir/"$name".text1 $DecompressDir/"$name".text2
		return
	fi

	for line in .gnu_debuglink .gnu_debugdata .ignore.note.gnu.build-id .note.gnu.build-id .rodata .data .text .rodata.str1.1 .rodata.str1.32 .rodata.str1.8 .rodata.str1.4
	do
		objdump -s -j $line  "$compare_dir_1"/"$file"  1> $DecompressDir/1.log 2> /dev/null
		if [ $? -eq 0 ]
		then
			objdump -s -j $line "$compare_dir_2"/"$file" 1> $DecompressDir/2.log 2> /dev/null
			sed -i '1,2d' $DecompressDir/1.log $DecompressDir/2.log
			diff $DecompressDir/1.log $DecompressDir/2.log &> /dev/null
			if [ $? -ne 0 ]
			then
				case $line in
				.gnu_debuglink)
					echo "二进制文件的调试文件的CRC32校验值差异;"
					;;
				.gnu_debugdata)
					echo "debugdata信息由于lzma压缩导致差异;"
					;;
				.note.gnu.build-id)
					echo "构建标识符差异;"
					;;
				.ignore.note.gnu.build-id)
					echo "构建标识符差异;"
					;;
				.rodata)
					find_elf_data_diff_reason "rodata"
					;;
				.rodata.str1.1)
					find_elf_data_diff_reason "rodata"
					;;
				.rodata.str1.4)
					find_elf_data_diff_reason "rodata"
					;;
				.rodata.str1.8)
					find_elf_data_diff_reason "rodata"
					;;
				.rodata.str1.32)
					find_elf_data_diff_reason "rodata"
					;;
				.data)
					find_elf_data_diff_reason "data"
					;;
				.text)
					find_elf_text_diff_reason "$file" "$compare_dir_1" "$compare_dir_2"
					;;
				esac
				clean_section $line "$file"
			fi
			rm -rf $DecompressDir/1.log $DecompressDir/2.log
		else
			rm -rf $DecompressDir/1.log
		fi
	done
	diff $DecompressDir/"$name".text1 $DecompressDir/"$name".text2 &> /dev/null
	if [ $? -eq 0 ]
	then
		rm -rf $DecompressDir/"$name".text1 $DecompressDir/"$name".text2
		return 0
	else
		rm -rf $DecompressDir/"$name".text1 $DecompressDir/"$name".text2
		echo "差异原因不明;"
		return 1
	fi
}
#对比二进制文件的差异
function compare_elf()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local diff_file_dir=""
	local name=""
	local diff_reason=""

	diff_reason=`find_elf_diff_reason "$file" $compare_dir_1 $compare_dir_2`
	diff_reason=`echo $diff_reason | sed 's/\n/ /g'`
	echo -e ""$file"\t"${diff_reason}"" | tee -a ${DifferentFile}
	return 1
}
#解压cpio压缩文件，并对比其中的文件
function compare_cpio()
{
	local file="$1"
	local path=`dirname "$file"`
	local name=`basename "$file"`
	local compare_dir_1=$2
	local compare_dir_2=$3
	local filelist1=${name}_list1
	local filelist2=${name}_list2

	cpio -t < "$compare_dir_1"/"$file" >> $DecompressDir/"$filelist1" 2>/dev/null
	cpio -t < "$compare_dir_2"/"$file" >> $DecompressDir/"$filelist2" 2>/dev/null

	mkdir -p $DecompressDir/${Compare1_dir}/"$name"
	pushd $DecompressDir/${Compare1_dir}/"$name" &>/dev/null
	cpio -di < "$compare_dir_1"/"$file" &>/dev/null
	popd &>/dev/null

	mkdir -p $DecompressDir/${Compare2_dir}/"$name"
	pushd $DecompressDir/${Compare2_dir}/"$name" &>/dev/null
	cpio -di < "$compare_dir_2"/"$file" &>/dev/null
	popd &>/dev/null

	compare_dir "$filelist1" "$filelist2" "$name"
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#将部分特殊，已有结论的文件输出到差异列表
function compare_spec_file()
{

	local file="$1"
	local diff_reason="$2"

	echo -e ""${file}"\t"$diff_reason"" | tee -a ${DifferentFile}
	return 1
}
#对比字符设备文件，主要是对比主设备号和次设备号
function compare_char_dev()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local device1=`ls -l "$compare_dir_1"/"$file" | awk '{print $5$6}'`
	local device2=`ls -l "$compare_dir_2"/"$file" | awk '{print $5$6}'`
	if [ "$device1" != "$device2" ]
	then
		echo -e ""${file}"\t"字符设备差异，差异原因不明;"" | tee -a ${DifferentFile}
		return 1
	else
		return 0
	fi
}
#对比data格式的文件
function compare_data()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local diff_file_dir=`dirname "${file}"`
	local name=`basename "$file"`

	compare_text "$file" "$compare_dir_1" "$compare_dir_2"
	if [ $? -eq 1 ]
	then
		return
	fi

	case "$name" in
		aux-cache|ld.so.cache)
		echo -e ""${file}"\t文件是通过Idconfig命令生成的临时的缓存文件，该文件内容是动态目录下的文件组成，对每个随机生成的hash值，然后根据hash值大小来进行排序写入，这就解释了不同机器上该文件的内容先后次序不一样;" | tee -a ${DifferentFile}
		return 1
		;;
		*.sig)
		echo -e ""${file}"\t.sig文件是用来给原文件进行签名加密的，里面存的是原文件的私钥，使用gpg命令进行加密和解密，里面有一段内容是用hash生成的一组随机数，因此两次编译生成的文件会有差异;" | tee -a ${DifferentFile}
		;;
		grub.der)
		echo -e ""${file}"\t该文件是通过openssl X509证书标准生成的DER格式的证书，里面的私钥是用hash生成的随机数，因此2次编译是不一样的;" | tee -a ${DifferentFile}
		;;
		omni.ja)
		compare_Zip "$file" "$compare_dir_1" "$compare_dir_2"
		;;
		*.js|*.jsm)
		echo -e ""${file}"\t时间戳差异;" | tee -a ${DifferentFile}
		;;
		modules.alias.bin)
		echo -e ""${file}"\t使用depmod命令导致差异;" | tee -a ${DifferentFile}
		;;
		*)
		echo -e ""${file}"\tdata文件差异，差异原因不明;" | tee -a ${DifferentFile}
		;;
	esac
}
#解压Zip压缩文件，并对比解压后的文件
function compare_Zip()
{
	local file="$1"
	local name=`basename "$file"`
	local compare_dir_1=$2
	local compare_dir_2=$3
	local filelist1=${name}_list1
	local filelist2=${name}_list2

	unzip -t $compare_dir_1/"$file" | grep "testing.*OK$" | sed 's/^[[:blank:]]*testing:[[:blank:]]//g' | sed 's/[[:blank:]]*OK$//g' >> $DecompressDir/"$filelist1"
	unzip -t $compare_dir_2/"$file" | grep "testing.*OK$" | sed 's/^[[:blank:]]*testing:[[:blank:]]//g' | sed 's/[[:blank:]]*OK$//g' >> $DecompressDir/"$filelist2"

	mkdir -p $DecompressDir/${Compare1_dir}/"$name"
	pushd $DecompressDir/${Compare1_dir}/"$name" &>/dev/null
	unzip $compare_dir_1/"$file" &>/dev/null
	popd &>/dev/null

	mkdir -p $DecompressDir/${Compare2_dir}/"$name"
	pushd $DecompressDir/${Compare2_dir}/"$name" &>/dev/null
	unzip $compare_dir_2/"$file" &>/dev/null
	popd &>/dev/null
	compare_dir "$filelist1" "$filelist2" "$name"
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#解压ar压缩文件，并对比解压后的文件，主要是静态库
function compare_ar()
{
	local file="$1"
	local name=`basename "$file"`
	local compare_dir_1=$2
	local compare_dir_2=$3
	local filelist1=${name}_list1
	local filelist2=${name}_list2

	mkdir -p $DecompressDir/${Compare1_dir}/"$name"
	pushd $DecompressDir/${Compare1_dir}/"$name" &>/dev/null
	cp "$compare_dir_1"/"$file" $DecompressDir/${Compare1_dir}/"$name"
	ar -x $name;rm -rf $name
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist1"
	popd &>/dev/null

	mkdir -p $DecompressDir/${Compare2_dir}/"$name"
	pushd $DecompressDir/${Compare2_dir}/"$name" &>/dev/null
	cp "$compare_dir_2"/"$file" $DecompressDir/${Compare2_dir}/"$name"
	ar -x $name;rm -rf $name
	find . | sed 's/^\.\///g' | sed 's/^\.//g' | sed '/^$/d' >> ${DecompressDir}/"$filelist2"
	popd &>/dev/null

	compare_dir "$filelist1" "$filelist2" "$name"
	if [ $? -eq 0 ]
	then
		return 0
	else
		echo "[ "${file}" ]" | tee -a ${DifferentFile}
		return 1
	fi
}
#分析文件格式，调用对应格式的差异对比函数
function compare_file()
{
	local file="$1"
	local compare_dir_1=$2
	local compare_dir_2=$3
	local name=`basename "$file"`

	#对比链接文件
	if [ -L ${compare_dir_1}/"$line" ] && [ -L ${compare_dir_2}/"$line" ]
	then
		#compare_link "$file" $compare_dir_1 $compare_dir_2
		return 0
	fi
	#如果是目录，调用对比目录的函数
	if [ -d $compare_dir_1/"$file" -a -d $compare_dir_2/"$file" ]
	then
		backup_dir "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#如果是字符设备|块设备|管道|套接字文件，直接跳过
	ls -l $compare_dir_1/"$file" | egrep "^c|^b|^p|^s" &> /dev/null && \
	ls -l $compare_dir_2/"$file" | egrep "^c|^b|^p|^s" &> /dev/null
	if [ $? -eq 0 ]
	then
		#compare_char_dev "$file" $compare_dir_1 $compare_dir_2
		#return $?
		return 0
	fi
	#如果文件无差异，可以直接返回
	diff ${compare_dir_1}/"${file}" ${compare_dir_2}/"${file}" &> /dev/null
	if [ $? -eq 0 ]
	then
		return 0
	fi

	#如果文件是.pyo或.pyc文件，直接写入差异列表
	echo "$file" | egrep "\.pyo$|\.pyc$" &> /dev/null
	if [ $? -eq 0 ]
	then
		echo -e ""${file}"\t差异原因不明;" | tee -a ${DifferentFile}
		return 1
	fi

	#如果是iso，调用对比iso的函数
	if [ ${file##*.} == "iso" ]
	then
		compare_iso "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#如果是tar包，调用对比tar的函数
	echo "${file}" | egrep "\.tar.gz$|\.tar.bz2$|\.tar.xz$|\.tar$|\.tgz$" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_tar "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#如果是rpm包，调用对比rpm包的函数
	file -b $compare_dir_1/"${file}" | egrep "^RPM|^DOS/MBR boot sector" &> /dev/null && \
	file -b $compare_dir_2/"${file}" | egrep "^RPM|^DOS/MBR boot sector" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_rpm "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比gzip压缩文件
	file -b $compare_dir_1/"$file" | grep "^gzip compressed data"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "^gzip compressed data"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_compress_file "gunzip" "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比bzip2压缩文件
	file -b $compare_dir_1/"$file" | grep "^bzip2 compressed data"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "^bzip2 compressed data"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_compress_file "bunzip2" "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比xz压缩文件,如initrd.img
	file -b $compare_dir_1/"$file" | grep "^XZ compressed data$"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "^XZ compressed data$"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_compress_file "unxz" "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比Zip压缩文件
	file -b $compare_dir_1/"$file" | egrep "^Zip archive data|^Zip data"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | egrep "^Zip archive data|^Zip data"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_Zip "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比Microsoft OOXML压缩文件
	file -b $compare_dir_1/"$file" | grep "Microsoft OOXML"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "Microsoft OOXML"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_Zip "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比ar压缩文件
	file -b $compare_dir_1/"$file" | grep "ar archive"  &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "ar archive"  &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_ar "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比cpio压缩文件
	file -b $compare_dir_1/"$file" | grep "cpio" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "cpio" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_cpio "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比text文件
	file -b $compare_dir_1/"$file" | grep "text" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "text" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_text "$file" $compare_dir_1 $compare_dir_2
		if [ $? -eq 0 ]
		then
			echo -e "$file" | tee -a ${DifferentFile}
		fi
		return 1
	fi
	#对比elf文件
	file -b $compare_dir_1/"$file" | grep "ELF" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "ELF" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_elf "$file" $compare_dir_1 $compare_dir_2
		return $?
	fi
	#对比rom文件
	file -b $compare_dir_1/"$file" | grep "ROM Ext" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "ROM Ext" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "调用rand函数，产生随机数导致的差异;"
		return $?
	fi
	#对比pickle文件
	if [ ${file##*.} == "pickle" ]
	then
		compare_spec_file "$file" "由Grammar.txt转换成pickle时，在处理过程中使用dict（字典），dict保存的数据是无序的，所以处理生成的数据文件每次编译都不同;"
		return $?
	fi
	#对比chk文件
	if [ ${file##*.} == "chk" ]
	then
		echo -e ""${file}"\t差异原因不明;" | tee -a ${DifferentFile}
		return 1 
	fi
	#对比GNU message catalog文件或Java的库文件,如blivet.mo|Version.class
	file -b $compare_dir_1/"$file" | grep "GNU message catalog" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "GNU message catalog" &> /dev/null
	if [ $? -eq 0 -o $name == "Version.class" -o $name == "version.class" ]
	then
		compare_spec_file "$file" "时间戳差异;"
		return $?
	fi
	#对比efi|COM executable for DOS|isolinux Loader|pxelinux loader|MS-DOS executable文件,或pxelinux.0,如grubx64.efi| |isolinux.bin|gpxelinux.0|syslinux.com
	file -b $compare_dir_1/"$file" | egrep "EFI application|COM executable for DOS|isolinux Loader|pxelinux loader|MS-DOS executable" &> /dev/null && \
	file -b $compare_dir_2/"$file" | egrep "EFI application|COM executable for DOS|isolinux Loader|pxelinux loader|MS-DOS executable" &> /dev/null
	if [ $? -eq 0 -o $name == "pxelinux.0" ]
	then
		compare_spec_file "$file" "编译时间和编译环境不同导致差异;"
		return $?
	fi
	#对比pdf或elc文件
	file -b $compare_dir_1/"$file" | grep "PDF document" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "PDF document" &> /dev/null
	if [ $? -eq 0 -o ${file##*.} == "elc" ]
	then
		compare_spec_file "$file" "时间戳和编译环境导致的差异;"
		return $?
	fi
	#对比Berkeley DB文件
	file -b $compare_dir_1/"$file" | grep "Berkeley DB" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "Berkeley DB" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "rpm数据库文件存在差异，由于数据库产生过程中头信息中存在唯一值，存储的rpm包信息时间戳存在差异"
		return $?
	fi
	#对比SQLite 数据库文件
	file -b $compare_dir_1/"$file" | grep "SQLite .* database" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "SQLite .* database" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "由于原文件的时间或环境差异导致对应数据库文件差异;"
		return $?
	fi
	#对比data文件
	file -b $compare_dir_1/"$file" | grep "^data$" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "^data$" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_data "$file" $compare_dir_1 $compare_dir_2
		return 1
	fi
	#对比Java KeyStore文件
	file -b $compare_dir_1/"$file" | grep "Java KeyStore" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "Java KeyStore" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "编译环境不同导致Java秘钥库差异;"
		return $?
	fi
	#对比SE Linux modular policy文件，如docker.pp
	file -b $compare_dir_1/"$file" | grep "SE Linux modular policy" &> /dev/null && \
	file -b $compare_dir_2/"$file" | grep "SE Linux modular policy" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "编译环境不同导致排序差异;"
		return $?
	fi
	#对比php script或COM executable (COM32R)文件，如phar.phar|sysdump.c32
	file -b $compare_dir_1/"$file" | egrep "^a /usr/bin/php script executable (binary data)$|COM executable (COM32R)" &> /dev/null && \
	file -b $compare_dir_2/"$file" | egrep "^a /usr/bin/php script executable (binary data)$|COM executable (COM32R)" &> /dev/null
	if [ $? -eq 0 ]
	then
		compare_spec_file "$file" "编译环境不同导致差异;"
		return $?
	fi

	echo -e ""${file}"\t差异原因不明;" | tee -a ${DifferentFile}
	return 1
}
#检查差异文件列表格式
function check_difference()
{
	if [ -e $DifferentFile ]
	then
		cat -n $DifferentFile | sort -rn | sed 's/^.\{7\}//g' > ${CompareDir}/real_difference 2>/dev/null
		cat ${CompareDir}/real_difference | uniq > $DifferentFile
		rm ${CompareDir}/real_difference
	fi
}

function fn_main()
{
	#检查脚本用法和入参
	usage $@
	#检查文件是否存在
	check_file_exist
	#清理环境
	clean_env
	#开始比较
	echo "Start compare $Compare1_dir/$Compare_file and $Compare2_dir/$Compare_file at `date`"
	compare_file "$Compare_file" "$CompareDir"/"$Compare1_dir" "$CompareDir"/"$Compare2_dir"
	#检查差异文件列表格式
	check_difference
}

fn_main "$@"
echo "compare finish at `date`!"
